package com.example.cookbook.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.cookbook.activity.MenuSearchActivity;
import com.example.cookbook.NetUtil;
import com.example.cookbook.R;
import com.example.cookbook.adapter.RecipeAdapter;
import com.example.cookbook.adapter.TagAdapter;
import com.example.cookbook.bean.RecipeBean;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class OneFragment extends Fragment {
    private  RecyclerView recy_view1;
    private RecyclerView recy_view_tags;
    private List<RecipeBean> recipeList = new ArrayList<>();   //定义食谱数组
    private String currentTag = TagAdapter.DEFAULT_TAGS[0];
    private EditText input_kc;     //定义搜索框对象
    private Button btn_search;     //定义搜索按钮对象
    public OneFragment() {
        // Required empty public constructor
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        //删除原来的TextView
        View view = inflater.inflate(R.layout.fragment_one, container, false);
        initView(view);
        return view;


    }

    private void initView(View view) {
        recy_view1 = (RecyclerView) view.findViewById(R.id.recy_view1);
        recy_view_tags = (RecyclerView) view.findViewById(R.id.recy_view_tags);
        //initView方法后追加
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recy_view_tags.setLayoutManager(layoutManager);
        recy_view_tags.setAdapter(new TagAdapter(this));
        input_kc = view.findViewById(R.id.input_kc);     //搜索框控件绑定
        btn_search = view.findViewById(R.id.btn_search);  //搜索按钮控件绑定

    }

    public void notifyOnTagClick(String tag) {
        currentTag = tag;
        initData();
    }
    /** @noinspection deprecation, deprecation */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();      //数据的初始化
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), MenuSearchActivity.class);
                String input_name=input_kc.getText().toString();
                intent.putExtra("search",input_name);
                getActivity().startActivity(intent);
            }
        });
    }
    private void initData() {
//访问网络  json解析====》数据
        NetUtil.queryRecipe(currentTag, new NetUtil.OnRecipeQueriedCallback() {
            @Override
            public void onSuccess(List<RecipeBean> recipes) {
                if (getActivity() == null)
                    return;
                recipeList.clear();
                recipeList.addAll(recipes);
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        RecipeAdapter adapter = new RecipeAdapter(getActivity(), recipeList);
                        //布局管理器让数据显示到recyclerview控件里面
                        StaggeredGridLayoutManager st = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
                        recy_view1.setLayoutManager(st);
                        recy_view1.setAdapter(adapter);
                    }
                });
            }
            //给“搜索”按钮设置点击事件


            @Override
            public void onFailure(Exception e) {
                if (getActivity() == null) return;
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getActivity(), "网络错误", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
}

